package com.whzykj.ctrl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Page;
import com.whzykj.common.DateUtil;
import com.whzykj.model.Article;
import com.whzykj.model.Dictionary;
import com.whzykj.model.GoodsInfo;
import com.whzykj.model.Notice;
import com.whzykj.model.Userinfo;

public class ArticleController extends Controller {
	public void index() {
		this.renderJsp("/admin/system/articleList.jsp");
	}
	
	/**
	 * 显示文章信息的列表
	 */
	public void list(){
		String sql="";
//		SELECT art.ARTICLE_ID,art.ARTICLE_TITLE,art.ARTICLE_KIND,art.REF_ID,art.VISIT_URL 
//		FROM article art
//		inner join dictionary dict on art.ARTICLE_KIND=dict.DICT_CODE
//		where dict.DICT_TYPE='ARTICLE_KIND' AND 
//		ORDER BY art.CREATE_TIME DESC
	}
	
	public void listArticle(){
		Integer page = this.getParaToInt("page");
		Integer rows = this.getParaToInt("rows");
		StringBuffer condition=new StringBuffer("where 1=1 ");
		String[] statusList={"TO_DAYS(NOW())<TO_DAYS(nt.START_DATE) ","TO_DAYS(NOW())>=TO_DAYS(nt.START_DATE) AND TO_DAYS(NOW())<=TO_DAYS(nt.END_DATE) ","TO_DAYS(NOW())>TO_DAYS(nt.END_DATE) "};
		Integer status=this.getParaToInt("status");
		Integer kind=this.getParaToInt("kind");
		String username=this.getPara("username");
		String sqlBefore = "select nt.*,ui.USERNAME  ";
		if(status!=null && status!=-1){
			condition.append(" and "+statusList[status-1]);
		}
		if(kind!=null && kind!=-1){
			condition.append(" AND nt.NOTICE_KIND=3 ");
		}
		if(username!=null && username.trim().length()!=0){
			condition.append(" AND ui.USERNAME like'%"+username+"%' ");
		}
		StringBuffer sqlAfter = new StringBuffer(
				"from notice nt ");
		sqlAfter.append("LEFT JOIN userinfo ui on nt.USERID=ui.USERID ");
		sqlAfter.append(condition.toString());
		sqlAfter.append(" order by nt.START_DATE DESC ");
		Page<Notice> paginate = Notice.dao.paginate(page, rows, sqlBefore,
				sqlAfter.toString());
		Map map = new HashMap();
		map.put("rows", paginate.getList());
		map.put("total", paginate.getTotalRow());
		this.renderJson(map);	
	}
	
	/**
	 * 获取文章信息列表
	 */
	public void findArticleKindList(){
		List<Dictionary> dictList=Dictionary.me.getDic("ARTICLE_KIND");
		this.renderJson(dictList);
	}
	
	/**
	 * 返回商品信息列表
	 */
	public void findGoodinfoList(){
		List<GoodsInfo> goodsList=GoodsInfo.me.find("SELECT gi.GOODS_ID,gi.GOODS_NAME,gi.APP_NAME from goodsinfo gi order by gi.GOODS_ID");
		this.renderJson(goodsList);
	}

	/**
	 * 进入到添加文章信息的列表页面
	 */
	public void addBefore() {
		Integer aKind = this.getParaToInt(0); // 获取传入的第一个参数，参数为文章的类型：1-公告信息；2-商品信息；3-帮助信息
		Integer refId = this.getParaToInt(1); // 获取传入的第二个参数，参数为关联的编号：如公告信息的编号、商品信息的编号、帮助信息的编号(1-购买流程、2-常见问题)
		String sql="select * from article where ARTICLE_KIND="+aKind+" and REF_ID="+refId;
		Article article=Article.dao.findFirst(sql);
		if(article==null){
			article=new Article();
			String titleName = null;
			switch (aKind) {
			case 1:
				Notice notice = Notice.dao.findById(refId);
				if (notice != null) {
					titleName = "公告：" + notice.getStr("NOTICE_TITLE") + "的描述";
				}
				break;
			case 2:
				GoodsInfo goodsinfo = GoodsInfo.me.findById(refId);
				if (goodsinfo != null) {
					titleName = "商品：" + goodsinfo.getStr("GOODS_NAME" + "的描述");
				}
				break;
			case 3:
				String[] helpinfo = { "购买流程", "常见问题" };
				titleName = "帮助：" + helpinfo[refId - 1] + "的描述";
				break;
			default:
				titleName = "未知信息";
			}
			article.set("ARTICLE_TITLE", titleName);
			article.set("ARTICLE_KIND", aKind);
			article.set("REF_ID", refId);
		}		
		this.setAttr("article", article);
		this.renderJsp("/admin/system/articleAdd.jsp");
	}

	/**
	 * 保存文章信息
	 */
	public void addArticle() {
		Article article = null;
		article = this.getModel(Article.class);
		Userinfo userinfo = (Userinfo) this.getSessionAttr("LOGIN_USER");
		if (userinfo == null) {
			this.renderJsp("/login.jsp");
			return;
		} 
		int aKind = article.getInt("ARTICLE_KIND");
		int refId = article.getInt("REF_ID");
		if(article.getInt("ARTICLE_ID")==null || article.getInt("ARTICLE_ID")<1){
			article.set("CREATE_TIME", DateUtil.getNow());
			String url = PropKit.use("config.properties").get("web.url"); // 获取访问路径：http://www.whbaicaiyuan.com
			article.set("ADD_USERID", userinfo.getInt("USERID"));
			switch (aKind) {
			case 1:
				url = url + "/wap/notice/" + refId;
				break;
			case 2:
				url = url + "/wap/goods/" + refId;
				break;
			case 3:
				url = url + "/wap/help/" + refId;
				break;
			default:
				url = url + "/wap/defaultPage";
			}
			article.set("VISIT_URL", url);
			boolean add=article.save();
			if(add && aKind==1){
				Notice notice=Notice.dao.findById(refId);
				notice.set("CONTENT_IMG_URL", article.get("VISIT_URL"));
				notice.update();
			}
		}else{
			article.set("UPDATE_USERID", userinfo.getInt("USERID"));
			article.set("UPDATE_TIME", DateUtil.getNow());
			article.update();
		}
		
		
		this.renderJsp("/admin/system/articleAdd.jsp");
	}
}
